<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-17px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          class="!w-240px"
          placeholder="请输入项目名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属地市" prop="region">
        <el-input
          v-model="queryParams.region"
          class="!w-240px"
          placeholder="请输入所属地市"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-select
          v-model="queryParams.tag"
          class="!w-240px"
          placeholder="请选择案例标签"
          @change="handleQuery"
          placement="bottom"
        >
          <el-option
            v-for="item in optionTag"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <!-- <el-button type="primary" plain @click="openAddDialog">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button> -->
        <el-button type="danger" v-if="userRoleId === 1" plain @click="allDelBtn">
          <Icon class="mr-5px" icon="ep:delete" />
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <div>
    <el-row :gutter="10">
      <el-col :xl="6" :lg="8" :md="12" :sm="12" :xs="24" v-for="item in list" :key="item">
        <el-card
          shadow="hover"
          class="mb-8px"
          @click="handleAudit(item)"
          style="position: relative"
        >
          <img class="picture" src="../../../assets/imgs/login/login-bg3.png" title="查看详情" />
          <el-checkbox
            v-if="userRoleId === 1"
            @click.stop
            style="position: absolute; left: 30px"
            v-model="item.checked"
          />
          <div class="flex my-10px">
            <div class="case_tag">{{ item.region }}</div>
            <div class="case_title" :title="item.projectName">
              {{ item.projectName }}
            </div>
          </div>
          <div class="label_style">
            <!-- <el-tag effect="light" class="tagstyle">{{ item.region }} </el-tag> -->
            <el-tag effect="light" v-for="tag in item.tagList" :key="tag" class="tagstyle"
              >{{ typeRelate(tag) }}
            </el-tag>
          </div>
        </el-card>
      </el-col>
      <ContentWrap class="w-100%" v-if="list.length === 0">
        <div class="w-100%">
          <el-empty description="暂无内容" />
        </div>
      </ContentWrap>
    </el-row>
    <!-- 分页 -->
    <el-pagination
      class="mt-35px text-14px"
      v-model:current-page="queryParams.pageNo"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      background
      :hide-on-single-page="value"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <CaseDetail v-model:visible="messageVisible" :editData="editData" />
  <AddDialog ref="addCase" @success="getList" />
</template>

<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as IctApi from '@/api/project/ict'
import * as DictDataApi from '@/api/system/dict/dict.data'
import { getUserProfile } from '@/api/system/user/profile'
import { ref, reactive, onMounted } from 'vue'
import CaseDetail from './caseDetail.vue'
import AddDialog from './components/addDialog.vue'
const message = useMessage()

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const messageVisible = ref(false)
const editData = ref({})
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectName: '',
  region: '',
  tag: ''
})
const addCase = ref()

const queryFormRef = ref()

//获取用户角色id
const userRoleId = ref()
const getUser = async () => {
  await getUserProfile().then((res) => {
    userRoleId.value = res.roles[0].id
  })
}

// 条数变化
const handleSizeChange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}

// 页码变化
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

// 获取案例标签
const optionTag = ref([])
const getTagData = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
    dictType: 'ict_caseLibrary_lable'
  }
  DictDataApi.getDictDataPage(params).then((res) => {
    optionTag.value = res.list
  })
}

// 标签字段映射
const typeRelate = (itemValue) => {
  const match = optionTag.value.find((t) => t.value === itemValue)
  return match ? match.label : itemValue
}

// 查询列表
const getList = async () => {
  IctApi.getCasePage(queryParams)
    .then((res) => {
      list.value = res.list || []
      list.value.forEach((item) => (item.tagList = item.tags.split(',')))
      total.value = res.total || 0
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

// 搜索按钮操作
const handleQuery = async () => {
  queryParams.pageNo = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.pageNo = 1
  getList()
}

// 详情
const handleAudit = (item: {}) => {
  editData.value = item
  editData.value.optionTag = optionTag.value
  messageVisible.value = true
}

// 新增案例
const openAddDialog = () => {
  addCase.value.open()
}

// 批量删除
const allDelBtn = async () => {
  let deleteArr = list.value.filter((item: any) => item.checked).map((item) => item.id)
  if (deleteArr.length == 0) {
    message.error('至少选中一条数据！')
  } else {
    ElMessageBox.confirm(`确认删除选中案例吗？删除当前选中数据无法恢复，请谨慎操作！`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        IctApi.deleteCaseList(deleteArr).then((res) => {
          if (res === true) {
            ElMessage.success('删除成功！')
            getList()
          }
        })
      })
      .catch(() => {
        ElMessage.info('已取消删除')
      })
  }
}

// 初始化
onMounted(() => {
  getList()
  getTagData()
  getUser()
})
</script>
<style scoped>
.el-pagination {
  justify-content: flex-end;
}

.picture {
  width: 100%;
  height: auto;
  overflow: hidden;
  cursor: pointer;
}

.tagstyle {
  height: 23px;
  margin-right: 10px;
}

.label_style {
  display: flex;
  overflow-x: scroll;
  overflow-y: hidden;
  padding: 6px 0;
}

:deep(.label_style)::-webkit-scrollbar {
  height: 3px;
}

:deep(.label_style):hover::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
}

:deep(.label_style)::-webkit-scrollbar-thumb {
  border-radius: 20px;
  background: transparent;
}

:deep(.label_style)::-webkit-scrollbar-track {
  display: none;
}

.case_tag {
  background-color: rgba(234, 241, 254, 1);
  color: rgb(19, 134, 250);
  font-size: 14px;
  text-align: center;
  padding: 2px 5px;
  margin-right: 5px;
}

.case_title {
  flex: 1 1;
  font-size: 16px;
  line-height: 24px;
  overflow: hidden;
  height: 24px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>